home *** CD-ROM | disk | FTP | other *** search
/ 500 MB Nyheder Direkte fra Internet 2 / 500 MB nyheder direkte fra internet CD 2.iso / start / data / text / cobol.txt < prev    next >
Text File  |  1994-09-21  |  69KB  |  1,847 lines

  1.  
  2.                  FUN
  3.   Like BASIC and PASCAL, COBOL is a computer language. ``COBOL'' 
  4. is pronounced ``koe ball'' and stands for ``COmmon Business 
  5. Oriented Language''.
  6.   COBOL solves business problems that involve large files of 
  7. data, so COBOL's used mainly by businesses having maxicomputers. 
  8. But today, you can use COBOL even on minicomputers and 
  9. microcomputers.
  10.   In the ``help wanted'' section of your local newspaper, many 
  11. ads that say ``programmer wanted'' are placed by businesses that 
  12. have maxicomputers and use COBOL. To get a job through the ``help 
  13. wanted'' section, a knowledge of COBOL will help you more than 
  14. PASCAL or FORTRAN.
  15.   The first version of COBOL was called COBOL 60, because it was 
  16. invented in 1960. Then came COBOL 61, COBOL 65, COBOL 68, COBOL 
  17. 74, and COBOL 85. Today, most computers still use COBOL 74 or a 
  18. variation of it. This chapter explains how to write COBOL 
  19. programs that work on most computers.
  20.   During the 1960's and early 1970's, COBOL programmers used a 
  21. style called ``easy programming''. Today, most COBOL programmers 
  22. use a more sophisticated style, called structured programming. 
  23. This chapter explains structured programming. Though it's harder 
  24. to learn than easy programming, it will make your boss kiss you.
  25.  
  26.            Simple programs
  27.   Every COBOL program is written as an outline. Here's a short 
  28. outline; to turn it into a COBOL program, just fill in the 
  29. blanks:
  30. IDENTIFICATION DIVISION.
  31. PROGRAM-ID.
  32.         The program's name.
  33. AUTHOR.
  34.         Your name.
  35.  
  36. ENVIRONMENT DIVISION.
  37. CONFIGURATION SECTION.
  38. SOURCE-COMPUTER.
  39.         The computer's name.
  40. OBJECT-COMPUTER.
  41.         The computer's name again.
  42.  
  43. DATA DIVISION.
  44.  
  45. PROCEDURE DIVISION.
  46. MAIN-ROUTINE.
  47.         What you want the computer to do.
  48.         STOP RUN.
  49.  
  50.                                          For example, here's a 
  51. COBOL program I wrote:
  52. Program                                                    Reason
  53. IDENTIFICATION DIVISION.
  54. PROGRAM-ID.
  55.         HARRY.                                             The 
  56. program's name is HARRY.
  57. AUTHOR.
  58.         RUSS WALTER.                                       My 
  59. name is Russ Walter.
  60.  
  61. ENVIRONMENT DIVISION.
  62. CONFIGURATION SECTION.
  63. SOURCE-COMPUTER.
  64.         DECSYSTEM-20.                                      My 
  65. computer's a DECsystem-20.
  66. OBJECT-COMPUTER.
  67.         DECSYSTEM-20.
  68.  
  69. DATA DIVISION.
  70.  
  71. PROCEDURE DIVISION.
  72. MAIN-ROUTINE.
  73.         DISPLAY "LIFE STINKS."                             I want 
  74. the computer to gripe.
  75.         STOP RUN.
  76.                                          When I run that program, 
  77. the computer will print:
  78. LIFE STINKS
  79.                                          Every COBOL program 
  80. consists of four parts. The first part of the program is called 
  81. the IDENTIFICATION DIVISION: it includes the program's name and 
  82. the programmer's name. The second part of the program is called 
  83. the ENVIRONMENT DIVISION: it includes the computer's name. The 
  84. third part of the program is called the DATA DIVISION; for a 
  85. simple program, the DATA DIVISION is blank. The fourth part of 
  86. the program is the PROCEDURE DIVISION: it says what you want the 
  87. computer to do.
  88.                                          The order is important: 
  89. the IDENTIFICATION DIVISION must come first, then the ENVIRONMENT 
  90. DIVISION, then the DATA DIVISION, and finally the PROCEDURE 
  91. DIVISION. So to become an expert COBOL programmer, you must 
  92. memorize: ``IDENTIFICATION, ENVIRONMENT, DATA, PROCEDURE''.
  93.                                          To memorize that easily, 
  94. memorize this easy sentence: ``I enjoy data processing''. In that 
  95. sentence, the words begin with the letters ``I E D P'' ___ and so 
  96. do the four COBOL divisions.
  97.                                          In the program, each 
  98. blank that you fill is called a paragraph. In the first 
  99. paragraph, write the program's name; in the next paragraph, write 
  100. your own name; in the next two paragraphs, write the computer's 
  101. name; and in the last paragraph, write what you want the computer 
  102. to do. The first paragraph is called the PROGRAM-ID; the next 
  103. paragraph is called the AUTHOR; the next two paragraphs are 
  104. called the SOURCE-COMPUTER and the OBJECT-COMPUTER; and the last 
  105. paragraph is called the MAIN-ROUTINE.
  106.                                          Each paragraph is 
  107. indented. To indent on PDP and Eclipse computers, type a 
  108. controlled I.
  109.                                          In COBOL, the only 
  110. important punctuation mark is the period. When writing a simple 
  111. program, put a period at the end of each line. COBOL never 
  112. requires commas or semicolons.
  113.                                          Don't forget the 
  114. hyphens! Put a hyphen in PROGRAM-ID, SOURCE-COMPUTER, 
  115. OBJECT-COMPUTER, and MAIN-ROUTINE.
  116.   Use correct spacing.
  117. Right:DISPLAY "BURP".
  118. Wrong:DIS PLAY "BURP".
  119. Wrong:DISPLAY"BURP".
  120. Wrong:DISPLAY "BURP" .
  121.   In the paragraphs that are called SOURCE-COMPUTER and 
  122. OBJECT-COMPUTER, you must type the computer's name correctly. 
  123. Here are the correct names for some famous computers:
  124. IBM-360.
  125. IBM-370.
  126. PDP-11.
  127. DECSYSTEM-10.(It means you have a PDP-10.)
  128. DECSYSTEM-20.(It means you have a PDP-20.)
  129. ECLIPSE C300.
  130. 6600.     (It means you have a CDC 6600.)
  131. Don't forget the hyphens!
  132.   Go ahead: try writing your own COBOL program. In the PROCEDURE 
  133. DIVISION, remember to say DISPLAY:
  134. BASIC:10 PRINT "LIFE STINKS"
  135. PASCAL:WRITELN('LIFE STINKS');
  136. COBOL:DISPLAY "LIFE STINKS".
  137.    Old-fashioned computers On IBM computers, instead of using 
  138. quotation marks, you must use apostrophes.
  139. Most computers:  DISPLAY "LIFE STINKS".
  140. IBM computers:   DISPLAY 'LIFE STINKS'.
  141.   On IBM and CDC computers, you must indent the entire program, 
  142. like this (each ■ represents a blank space):
  143. 7 blanks  8th column
  144.  
  145. ■■■■■■■IDENTIFICATION DIVISION.
  146. ■■■■■■■PROGRAM-ID.
  147. ■■■■■■■■■■■HARRY.
  148.  
  149. 7 blanks 4 blanks
  150.  
  151.  11 blanks       12th column
  152. On those computers, each paragraph begins in column 12, and must 
  153. not go farther to the right than column 72. (The computer ignores 
  154. everything in columns 73-80.)
  155.  
  156.                    Abridgments
  157.   If you're lazy, you can omit the AUTHOR paragraph:
  158. Complete IDENTIFICATION DIVISIONAbridged version
  159. IDENTIFICATION DIVISION.  IDENTIFICATION DIVISION.
  160. PROGRAM-ID.               PROGRAM-ID.
  161.         HARRY.                    HARRY.
  162. AUTHOR.
  163.         RUSS WALTER.
  164.   If you're lazy, and you're using a PDP-10, PDP-20, Eclipse, or 
  165. IBM computer, you can omit the CONFIGURATION SECTION, 
  166. SOURCE-COMPUTER, and OBJECT-COMPUTER:
  167. Complete ENVIRONMENT DIVISIONAbridged version
  168. ENVIRONMENT DIVISION.     ENVIRONMENT DIVISION.
  169. CONFIGURATION SECTION.
  170. SOURCE-COMPUTER.
  171.         The computer's name.
  172. OBJECT-COMPUTER.
  173.         The computer's name.
  174.   If you're very lazy, and you're using a PDP-10 or PDP-20 
  175. computer, you can abridge the program even further, so that the 
  176. entire program looks like this:
  177. IDENTIFICATION DIVISION.
  178. PROCEDURE DIVISION.
  179.         DISPLAY "LIFE STINKS".
  180.         STOP RUN.
  181.   But if you're working for a big company, your employer will 
  182. expect you to not be lazy: if you're lazy, you get fired!
  183.                                                         Fancy displays
  184.                                                      You've seen 
  185. that every COBOL program consists of four divisions: 
  186. IDENTIFICATION, ENVIRONMENT, DATA, and PROCEDURE. The most 
  187. important division is the PROCEDURE DIVISION. Let's look at it 
  188. more closely.
  189.                                                      Here's a 
  190. cute PROCEDURE DIVISION:
  191. PROCEDURE DIVISION.
  192. MAIN-ROUTINE.
  193.         DISPLAY "BILLIE AND BONNIE".
  194.         DISPLAY "BURP".
  195.         STOP RUN.
  196.                                                      It makes the 
  197. computer display:
  198. BILLIE AND BONNIE
  199. BURP
  200.                                                      Another 
  201. example:
  202. PROCEDURE DIVISION.
  203. MAIN-ROUTINE.
  204.         DISPLAY "FLU" "SHED".
  205.         STOP RUN.
  206. The computer will display FLU and SHED on the same line:
  207. FLUSHED
  208.  
  209.                PERFORM
  210.   Let's make the computer display ``I LOVE YOU'', then display 
  211. ``I HATE YOU'' six times, then display ``I AM CONFUSED'', like 
  212. this:
  213. I LOVE YOU
  214. I HATE YOU
  215. I HATE YOU
  216. I HATE YOU
  217. I HATE YOU
  218. I HATE YOU
  219. I HATE YOU
  220. I AM CONFUSED
  221.   Here's the PROCEDURE DIVISION:
  222. PROCEDURE DIVISION.
  223. MAIN-ROUTINE.
  224.         DISPLAY "I LOVE YOU".
  225.         PERFORM EXPRESS-THE-HATRED 6 TIMES.
  226.         DISPLAY "I AM CONFUSED".
  227.         STOP RUN.
  228. EXPRESS-THE-HATRED.
  229.         DISPLAY "I HATE YOU".
  230.   That PROCEDURE DIVISION consists of two paragraphs. The first 
  231. paragraph is called the MAIN-ROUTINE. The second paragraph is 
  232. called EXPRESS-THE-HATRED.
  233.   The computer obeys the first paragraph: it displays ``I LOVE 
  234. YOU'', then performs the EXPRESS-THE-HATRED 6 times, then 
  235. displays ``I AM CONFUSED'', and finally stops.
  236.   When you invent your own PROCEDURE DIVISION, the first 
  237. paragraph should be called the MAIN-ROUTINE; for the other 
  238. paragraphs underneath, invent whatever names you like (such as 
  239. EXPRESS-THE-HATRED). A paragraph's name should be hyphenated, and 
  240. should contain no more than 30 characters. (EXPRESS-THE-HATRED 
  241. contains 18 characters, so it's okay.) The first paragraph is the 
  242. main routine, the paragraphs underneath are subroutines. The 
  243. bottom line of the main routine should say STOP RUN. In the 
  244. middle of the main routine, you should say to PERFORM the 
  245. subroutines.
  246.   In the example above, the main routine says to PERFORM the 
  247. EXPRESS-THE-HATRED subroutine 6 times. If you'd like to see more 
  248. hatred, say 100 times instead of 6:
  249.         PERFORM EXPRESS-THE-HATRED 100 TIMES.
  250. If you'd rather see just a little hatred, say just ___ 
  251.         PERFORM EXPRESS-THE-HATRED 1 TIMES.
  252. or say just:
  253.         PERFORM EXPRESS-THE-HATRED.
  254.   Let's make the computer display:
  255. I KNOW THAT
  256. YOU ARE DRIVING
  257. ME CRAZY
  258. YOU ARE DRIVING
  259. ME CRAZY
  260. YOU ARE DRIVING
  261. ME CRAZY
  262. YOU ARE DRIVING
  263. ME CRAZY
  264. YOU ARE DRIVING
  265. ME CRAZY
  266. AND YET I LOVE YOU
  267.  
  268. Here's the PROCEDURE DIVISION:
  269. PROCEDURE DIVISION.
  270. MAIN-ROUTINE.
  271.         DISPLAY "I KNOW THAT".
  272.         PERFORM ACT-AS-IF-GOING-INSANE 5 TIMES.
  273.         DISPLAY "AND YET I LOVE YOU".
  274.         STOP RUN.
  275. ACT-AS-IF-GOING-INSANE.
  276.         DISPLAY "YOU ARE DRIVING".
  277.         DISPLAY "ME CRAZY".
  278.                                          Let's make the computer 
  279. display:
  280. THE ASTRONAUTS GO
  281. UP
  282. UP
  283. UP
  284. UP
  285. UP
  286. UP
  287. UP
  288. AND THEN THEY COME
  289. DOWN
  290. DOWN
  291. DOWN
  292. DOWN
  293. DOWN
  294. DOWN
  295. DOWN
  296. Here's the PROCEDURE DIVISION:
  297. PROCEDURE DIVISION.
  298. MAIN-ROUTINE.
  299.         DISPLAY "THE ASTRONAUTS GO".
  300.         PERFORM SHOW-THE-ASTRONAUTS-RISING 7 TIMES.
  301.         DISPLAY "AND THEN THEY COME".
  302.         PERFORM SHOW-THE-ASTRONAUTS-FALLING 7 TIMES.
  303.         STOP RUN.
  304. SHOW-THE-ASTRONAUTS-RISING.
  305.         DISPLAY "UP".
  306. SHOW-THE-ASTRONAUTS-FALLING.
  307.         DISPLAY "DOWN".
  308.                                          Let's make the computer 
  309. display:
  310. YOU ARE VERY SWEET
  311. HA-HA-HA!
  312. HO-HO-HO!
  313. YOU CANNOT BE BEAT
  314. HA-HA-HA!
  315. HO-HO-HO!
  316. YOUR LIPS ARE LIKE WINE
  317. HA-HA-HA!
  318. HO-HO-HO!
  319. BUT YOU SMELL LIKE TURPENTINE
  320. HA-HA-HA!
  321. HO-HO-HO!
  322. YOU STINK!
  323. Here's the PROCEDURE DIVISION:
  324. PROCEDURE DIVISION.
  325. MAIN-ROUTINE.
  326.         DISPLAY "YOU ARE VERY SWEET".
  327.         PERFORM LAUGH-A-LOT.
  328.         DISPLAY "YOU CANNOT BE BEAT".
  329.         PERFORM LAUGH-A-LOT.
  330.         DISPLAY "YOUR LIPS ARE LIKE WINE".
  331.         PERFORM LAUGH-A-LOT.
  332.         DISPLAY "BUT YOU SMELL LIKE TURPENTINE".
  333.         PERFORM LAUGH-A-LOT.
  334.         DISPLAY "YOU STINK!".
  335.         STOP RUN.
  336. LAUGH-A-LOT.
  337.         DISPLAY "HA-HA-HA!".
  338.         DISPLAY "HO-HO-HO!".
  339.  
  340.  
  341.                     VARIABLES
  342.   Like other languages, COBOL lets you use variables. The name of 
  343. a variable can be a letter (like X or Y) or a hyphenated phrase 
  344. (like NUMBER-OF-BULLIES-I-SQUIRTED). A hyphenated phrase can have 
  345. up to 30 characters.
  346.   To use a variable, you must describe it in the data division, 
  347. as in this example:
  348. IDENTIFICATION DIVISION.
  349. PROGRAM-ID.
  350.         JUNK.
  351. AUTHOR.
  352.         RUSS WALTER.Instead of ``RUSS WALTER'', write your own 
  353. name.
  354.  
  355. ENVIRONMENT DIVISION.
  356. CONFIGURATION SECTION.
  357. SOURCE-COMPUTER.
  358.         DECSYSTEM-20.Instead of ``DECSYSTEM-20'', write your 
  359. computer's name.
  360. OBJECT-COMPUTER.
  361.         DECSYSTEM-20.
  362.  
  363. DATA DIVISION.
  364. WORKING-STORAGE SECTION.In the DATA DIVISION, say WORKING-STORAGE 
  365. SECTION.
  366. 01      K PICTURE IS XXX.
  367.  
  368. PROCEDURE DIVISION.
  369. MAIN-ROUTINE.
  370.         MOVE "HER" TO K.
  371.         DISPLAY "PUS" K "S".
  372.         STOP RUN.
  373.   In the DATA DIVISION's WORKING-STORAGE SECTION, the ``01      
  374. K'' says K is a variable. The PICTURE IS XXX says K is a string 
  375. that has three characters; each X stands for a character. In the 
  376. PROCEDURE DIVISION, the first sentence makes K become this 
  377. 3-character string: ``HER''. The next sentence makes the computer 
  378. display:
  379. PUSHERS
  380.   When you type that program, make sure you put a hyphen between 
  381. WORKING and STORAGE. If you forget the hyphen, the computer will 
  382. act crazy, and will say that your program contains many, many 
  383. errors.
  384.   Suppose you change the picture from XXX to XX, so that the DATA 
  385. DIVISION and PROCEDURE DIVISION look like this:
  386. DATA DIVISION
  387. WORKING-STORAGE SECTION
  388. 01      K PICTURE IS XX.
  389.  
  390. PROCEDURE DIVISION.
  391. MAIN-ROUTINE.
  392.         MOVE "HER" TO K.
  393.         DISPLAY "PUS" K "S".
  394.         STOP RUN.
  395. K will be a string having only two characters. When the computer 
  396. tries to move ``HER'' to K, only the first two characters of 
  397. ``HER'' will fit, so K will be ``HE''. The computer will display:
  398. PUSHES
  399.   Suppose you change the picture to XXXX. K will have four 
  400. characters. When the computer tries to move ``HER'' to K, it 
  401. needs to move a fourth character also, so it moves a blank space 
  402. at the end, which makes K be ``HER ''. The computer will display:
  403. PUSHER S
  404.  
  405.  
  406.   This program shows how revolutionary politics lead to 
  407. revolutionary clothing:
  408. DATA DIVISION.
  409. WORKING-STORAGE SECTION.
  410. 01      FIRST-PRESIDENT PICTURE IS XXXXXXXXXX.
  411. 01      CLEANING-METHOD PICTURE IS XXXX.
  412.  
  413. PROCEDURE DIVISION.
  414. MAIN-ROUTINE.
  415.         MOVE "WASHINGTON" TO FIRST-PRESIDENT.
  416.         MOVE FIRST-PRESIDENT TO CLEANING-METHOD.
  417.         DISPLAY CLEANING-METHOD " MY BLUE JEANS".
  418.         STOP RUN.
  419. Above the DATA DIVISION, write your own IDENTIFICATION DIVISION 
  420. and ENVIRONMENT DIVISION. The DATA DIVISION says FIRST-PRESIDENT 
  421. will be a string having ten characters, and CLEANING-METHOD will 
  422. be a string having four. The first sentence of the MAIN-ROUTINE 
  423. makes FIRST-PRESIDENT be ``WASHINGTON''. The next sentence tries 
  424. to move ``WASHINGTON'' to CLEANING-METHOD; but because of 
  425. CLEANING-METHOD's picture, the computer moves ``WASH'' instead. 
  426. The computer will display:
  427. WASH MY BLUE JEANS
  428.   To make sure you understand the word MOVE, examine this 
  429. example:
  430. DATA DIVISION.
  431. WORKING-STORAGE SECTION.
  432. 01      C PICTURE IS XXX.
  433. 01      D PICTURE IS XXX.
  434.  
  435. PROCEDURE DIVISION.
  436. MAIN-ROUTINE.
  437.         MOVE "CAT" TO C.C becomes ``CAT''.
  438.         MOVE C TO D.D becomes ``CAT''.
  439.         DISPLAY C.Since C is still ``CAT'', the computer displays 
  440. ``CAT''.
  441.         MOVE "HE" TO C.C becomes ``HE ''.
  442.         DISPLAY C "BLED".The computer displays ``HE BLED''.
  443.         STOP RUN.
  444.   COBOL allows abbreviations. You can say PIC instead of PICTURE 
  445. IS, and X(7) instead of XXXXXXX.
  446.  
  447.                 Numeric variables
  448.   Let's make the computer add 53 and 4, and display the sum, 57. 
  449. Here's how:
  450. DATA DIVISION.
  451. WORKING-STORAGE SECTION.
  452. 01      K PIC 99.
  453.  
  454. PROCEDURE DIVISION.
  455. MAIN-ROUTINE.
  456.         COMPUTE K = 53 + 4.
  457.         DISPLAY K.
  458.         STOP RUN.
  459. The PIC 99 says K is a number having two digits. (Each 9 stands 
  460. for a digit.) The MAIN-ROUTINE sets K equal to 53 + 4, which is 
  461. 57. The computer will display:
  462. 57
  463.   In the COMPUTE statement, the equal sign and the plus sign must 
  464. be surrounded by spaces.
  465. Right:  COMPUTE K = 53 + 4.
  466. Wrong:  COMPUTE K=53+4.
  467. Wrong:  COMPUTE K = 53+4.
  468.   If you change the picture to 999, K will be a number having 
  469. three digits. It will be 057 instead of 57. The computer will 
  470. display:
  471. 057
  472. (Exception: PDP-10 and PDP-20 computers are lazy; they don't 
  473. bother to display the 0 at the left; they display just 57.)
  474.   If you change the picture to 9, K will be a number having only 
  475. one digit. so K will not be 57. The computer will not display the 
  476. correct sum.
  477.                                                      Like 
  478. FORTRAN, COBOL uses these operators:
  479. Operator                                                   
  480. Meaning
  481. +                                                          plus
  482. -                                                          minus
  483. *                                                          times
  484. /                                                          
  485. divided by
  486. **                                                         
  487. exponent
  488.                                                      You must put 
  489. a blank space before and after each operator:
  490. Right                                                      Wrong
  491. 53 + 4                                                     53+4
  492. 7 ** 2                                                     7**2
  493. - J + 3                                                    -J + 3
  494.                                                      Like other 
  495. computer languages, COBOL lets you use parentheses. Do not put a 
  496. space after a left parenthesis:
  497. Right
  498. COMPUTE K = I * (- J + 3)
  499.  
  500.                 no space  spaces
  501.                                                      You can use 
  502. these short cuts:
  503. Sentence                                                       
  504. Short cut
  505. COMPUTE A = A + 7.                                             
  506. ADD 7 TO A.
  507. COMPUTE B = B - 4.                                             
  508. SUBTRACT 4 FROM B.
  509.                                                      Operators 
  510. are allowed only in sentences that say COMPUTE, IF, UNTIL, or 
  511. WHEN.
  512. Allowed:                                                   
  513. COMPUTE A = 2 * 3.
  514. Not allowed:                                               
  515. DISPLAY 2 * 3.
  516. Not allowed:                                               MOVE 2 
  517. * 3 TO A.
  518. Not allowed:                                               ADD 2 
  519. * 3 TO A.
  520.  
  521.               Decimals
  522.   You can use decimals:
  523. DATA DIVISION.
  524. WORKING-STORAGE SECTION.
  525. 01      K PIC 9999V99.
  526.  
  527. PROCEDURE DIVISION.
  528. MAIN-ROUTINE.
  529.         COMPUTE K = 2.208 + 4.109.
  530.         DISPLAY K.
  531.         STOP RUN.
  532. The PIC 9999V99 says K is a number having four digits, followed 
  533. by a decimal point, followed by two digits. (The V stands for the 
  534. decimal point.) The MAIN-ROUTINE tries to set K equal to 2.208 + 
  535. 4.109, which is 6.317; but because of K's picture, I will be 
  536. 0006.31 instead. So the computer should display 0006.31.
  537.   PDP-10 and PDP-20 computers don't bother to display the zeros: 
  538. they display 6.31. PDP-11, IBM, CDC, and Eclipse computers don't 
  539. bother to display the decimal point: they display 000631.
  540.   You can change that program by saying ROUNDED:
  541.         COMPUTE K ROUNDED = 2.208 + 4.109.
  542. The computer will find the sum (6.317), and round it so K is 
  543. 0006.32 instead of 0006.31.
  544.   If J's picture is 99, saying ``COMPUTE J = 200 / 3'' makes J be 
  545. 66. Saying ``COMPUTE J ROUNDED = 200 / 3'' makes J be 67.
  546.   Which is better: saying ``COMPUTE X = 4.9'' or ``MOVE 4.9 TO 
  547. X''? You should usually say ``MOVE 4.9 TO X'', because the 
  548. computer handles it more quickly. But MOVE cannot round; so if 
  549. you want to round, say COMPUTE.
  550.   MOVE can do strange things:
  551. DATA DIVISION.
  552. WORKING-STORAGE SECTION.
  553. 01      L PIC 999V99.
  554.  
  555. PROCEDURE DIVISION.
  556. MAIN-ROUTINE.
  557.         MOVE 16725.048 TO L.
  558.         DISPLAY L.
  559.         STOP RUN.
  560. L's picture makes the computer move the three digits just left of 
  561. the decimal point and the two digits just right of it. L will be 
  562. 725.04.
  563.  
  564.               Negatives
  565.   You can use negative numbers:
  566. DATA DIVISION.
  567. WORKING-STORAGE SECTION.
  568. 01      K PIC S9999.
  569.  
  570. PROCEDURE DIVISION.
  571. MAIN-ROUTINE.
  572.         COMPUTE K = 100 - 367.
  573.         DISPLAY K.
  574.         STOP RUN.
  575. In K's picture, the S stands for a sign (which can be plus or 
  576. minus). K will be -0267. The computer should display -0267.
  577.   PDP-10 and PDP-20 computers don't bother to display the zero: 
  578. they display -267. Some other computers display the minus sign on 
  579. top of the right digit, like this: 0267. On many computers, the 
  580. minus combines with the 7 and forms a P, like this: 026P.
  581.   If you omit the S from K's picture, K will be 0267 instead of 
  582. -0267.
  583.                                          To find the negative of 
  584. a power, use parentheses:
  585.         COMPUTE A = - (3 ** 2).
  586. If you omit the parentheses, the computer will get the wrong 
  587. answer.
  588.  
  589.                                                       ACCEPT
  590.                                          Here's how to translate 
  591. the BASIC word INPUT into PASCAL and COBOL.
  592. BASIC:  INPUT K
  593. PASCAL: READ(K);
  594. COBOL:  ACCEPT K.
  595.                                          Let's look at a COBOL 
  596. example:
  597. DATA DIVISION.
  598. WORKING-STORAGE SECTION.
  599. 01      K PIC XXX.
  600.  
  601. PROCEDURE DIVISION.
  602. MAIN-ROUTINE.
  603.         DISPLAY "THIS PROGRAM WANTS YOU TO TYPE SOMETHING".
  604.         ACCEPT K.
  605.         DISPLAY K.
  606.         STOP RUN.
  607.                                          The computer displays:
  608. THIS PROGRAM WANTS YOU TO TYPE SOMETHING
  609.                                          The statement ACCEPT K 
  610. makes the computer wait for you to type something. If you type 
  611. ___ 
  612. FIGHT
  613. the computer will try to move ``FIGHT'' to K; but since K's 
  614. picture is XXX, K will be ``FIG''. The computer will display:
  615. FIG
  616.                                          If you type ___ 
  617. ME
  618. the computer will try to move ``ME'' to K; since K's picture is 
  619. XXX, K will be ``ME ''. The computer will display:
  620. ME
  621.                                          Suppose a program says L 
  622. PIC 999 and ACCEPT L. If you input ___ 
  623. 4
  624. a PDP-10 or PDP-20 computer will make L be 004, but an IBM or CDC 
  625. computer will make L be 400.
  626.                                          Suppose a program says M 
  627. PIC S9999V99 and ACCEPT M. If you want M to be -0034.27, here's 
  628. what to input. . . . 
  629. On PDP-10 & PDP-20 computers: -0034.27 or -34.27
  630. On IBM and CDC computers:     003427
  631.  
  632.                      Editing
  633.   The computer can edit the output:
  634. DATA DIVISION.
  635. WORKING-STORAGE SECTION.
  636. 01      K PIC XXXBXXX.
  637.  
  638. PROCEDURE DIVISION.
  639. MAIN-ROUTINE.
  640.         MOVE "HITHER" TO K.
  641.         DISPLAY K.
  642.         STOP RUN.
  643.   In K's picture, B means a blank space. So when the computer 
  644. moves ``HITHER'', K becomes ``HIT HER''. The computer will 
  645. display:
  646. HIT HER
  647.   This program displays the Boston Computer Society's phone 
  648. number:
  649. DATA DIVISION.
  650. WORKING-STORAGE SECTION.
  651. 01      PHONE-NUMBER PIC 999B9999.
  652.  
  653. PROCEDURE DIVISION.
  654. MAIN-ROUTINE.
  655.         MOVE 3678080 TO PHONE-NUMBER.
  656.         DISPLAY PHONE-NUMBER.
  657.         STOP RUN.
  658. When the computer moves 3678080, PHONE-NUMBER becomes ``367 
  659. 8080''. The computer will display:
  660. 367 8080
  661.   This program is of historical importance:
  662. DATA DIVISION.
  663. WORKING-STORAGE SECTION.
  664. 01      K PIC 9B9B9999.
  665.  
  666. PROCEDURE DIVISION.
  667. MAIN-ROUTINE.
  668.         COMPUTE K = 741775 + 1.
  669.         DISPLAY "THE DECLARATION OF INDEPENDENCE WAS SIGNED ON " 
  670. K.
  671.         STOP RUN.
  672. The computer will display:
  673. THE DECLARATION OF INDEPENDENCE WAS SIGNED ON 7 4 1776
  674.   Using B to insert a blank is called editing. You've learned 
  675. four kinds of variables:
  676. Kind of variableSymbols in picture
  677. string      X
  678. edited stringX B
  679. number      9 V S
  680. edited number9 B Z , $ * . + - DB CR
  681.   You can use these pictures for editing numbers:
  682. K's pictureMeaning    What K will be if you move 50320 to KWhat K 
  683. will be if you move 0 to K
  684. B99999999a blank then eight digits" 00050320"   " 00000000"
  685. ZZZZZZZZZblanks then digits"    50320"          "         "
  686. $,$$$,$$$put $ before the digits"  $50,320"     "         "
  687. *,***,***stars instead of blanks"***50,320"     "*********"
  688.   To edit decimals, put a decimal point in the picture.
  689. K's pictureIf you move 50320.6 to KIf you move .04 to KIf you 
  690. move 0 to K
  691. Z,ZZZ,ZZZ.ZZ"   50,320.60""         .04""            "
  692. $,$$$,$$$.$$"  $50,320.60""        $.04""            "
  693. *,***,***.**"***50,320.60""*********.04""*********.**"
  694.   With those pictures, if you move 0 to K the computer doesn't 
  695. put any digits in K. To guarantee that K contains a digit, put 9 
  696. in the picture:
  697. K's pictureIf you move 50320.6 to KIf you move .04 to KIf you 
  698. move 0 to K
  699. Z,ZZZ,ZZ9.99"   50,320.60""        0.04""        0.00"
  700. $,$$$,$$9.99"  $50,320.60""       $0.04""       $0.00"
  701. *,***,**9.99"***50,320.60""********0.04""********0.00"
  702.  
  703.   To edit negative numbers, use +, -, DB, or CR.
  704. K's pictureMeaning      If you move -2.6 to KIf you move 2.6 to K
  705. ZZZ.ZZ+ put - or + afterwards"  2.60-"  "  2.60+"
  706. ZZZ.ZZ- put - or blank afterwards"  2.60-""  2.60 "
  707. ZZZ.ZZDBif negative, put DB (for debit)"  2.60DB""  2.60  "
  708. ZZZ.ZZCRif negative, put CR (for credit)"  2.60CR""  2.60  "
  709. For fancier pictures, replace the Z by $, *, or 9.
  710.   Here's how to put the sign before the digits:
  711. K's pictureMeaning    If you move -2.6 to KIf you move 2.6 to K
  712. +++.++  put - or + before digits" -2.60"" +2.60"
  713. ---.--  - or blank before digits" -2.60""  2.60"
  714.   Here are the differences between numbers and edited numbers:
  715.                         Number              Edited number
  716. how to put decimal point in the pictureV    .
  717.  
  718. how to put a sign in the pictureS           +, -, DB, or CR
  719.  
  720. how to fill up most of the picture9         9, Z, $, *, +, or -
  721.  
  722. what the value should beintermediate result in long 
  723. calculationthe final answer to be displayed
  724.  
  725. If the value's called K, can you say ACCEPT K?yesno
  726.  
  727. If the value's called K, can you use K in furtheryesno
  728. computations (such as COMPUTE L = K + 1)?
  729.  
  730. What happens if you try to DISPLAY the value?the ``0'', ``.'', 
  731. and ``-'' might look wrong                  displays correctly
  732.  
  733.              DECIMAL-POINT IS COMMA
  734.   Some Europeans write commas instead of decimal points, and 
  735. write decimal points instead of commas.
  736. United States and England:5,243,794.95
  737. France and Italy:5.243.794,95
  738. Germany:      5 243 794,95
  739.   To write a COBOL program for a Frenchman, an Italian, or a 
  740. German, make three changes. . . . 
  741.   Change #1 Insert this line:
  742.         DECIMAL-POINT IS COMMA.
  743. Put that line in a SPECIAL-NAMES paragraph, at the end of the 
  744. ENVIRONMENT DIVISION's CONFIGURATION SECTION:
  745. CONFIGURATION SECTION.
  746. SOURCE-COMPUTER.
  747.         The computer's name.
  748. OBJECT-COMPUTER.
  749.         The computer's name again.
  750. SPECIAL-NAMES.
  751.         DECIMAL-POINT IS COMMA.
  752.   Change #2 Type all numbers in French-Italian-German notation. 
  753. So instead of typing ___ 
  754.         MOVE 5243794.95 TO K
  755. type:
  756.         MOVE 5243794,95 TO K
  757.   Change #3 Use French-Italian-German notation in pictures for 
  758. edited numbers.
  759. For a Frenchman or an Italian:K PIC Z.ZZZ.ZZZ,ZZ
  760. For a German:   K PIC ZBZZZBZZZ,ZZ
  761.   Those are the only changes Europeans make. They still put a 
  762. period at the end of every sentence, and still use English COBOL 
  763. words such as MOVE and DISPLAY.
  764. American COBOL:DISPLAY "HELLO, STUPID".
  765. French COBOL:DISPLAY "BONJOUR, BETE".
  766. German COBOL:DISPLAY "GUTEN TAG, DUMMKOPF".
  767.  
  768.  
  769.                       LOGIC
  770.   COBOL lets you do logic.
  771.  
  772.                        IF
  773.   Like other computer languages, COBOL uses the word IF:
  774. BASIC                   PASCAL  COBOL
  775. INPUT I                 READ(I);ACCEPT I.
  776. IF I>5 THEN J=80: K=90  IF I>5 THENIF I > 5
  777.                            BEGIN;        MOVE 80 TO J
  778.                            J:=80;        MOVE 90 TO K.
  779.                            K:=90;
  780.                            END;
  781.   COBOL also lets you say ELSE:
  782. BASIC                   PASCAL  COBOL
  783. INPUT I                 READ(I);ACCEPT I.
  784. IF I>5 THEN J=80: K=90 ELSE J=30: K=50IF I>5 THENIF I > 5
  785.                            BEGIN;        MOVE 80 TO J
  786.                            J:=80;        MOVE 90 TO K
  787.                            K:=90;ELSE
  788.                            END          MOVE 30 TO J
  789.                         ELSE            MOVE 50 TO K.
  790.                            BEGIN;
  791.                            J:=30;
  792.                            K:=50;
  793.                            END;
  794.   In COBOL, when you write the IF statement, do not put a period 
  795. at the end of every line; instead, put the period just at the end 
  796. of the entire IF idea.
  797.   Notice that I indented the word MOVE. The indentation is 
  798. optional, but is a good habit. To indent on PDP and Eclipse 
  799. computers, type a controlled I; to indent on IBM and CDC 
  800. computers, press the space bar several times.
  801.   COBOL uses these IF lines:
  802. IF line Meaning
  803. IF I = 5If I is equal to 5
  804. IF I NOT = 5If I is not equal to 5
  805. IF I > 5If I is greater than 5
  806. IF I NOT > 5If I is not greater than 5
  807. IF I < 5If I is less than 5
  808. IF I NOT < 5IF I is not less than 5
  809.   You can use the words AND and OR and abbreviate:
  810. IF line:IF J > 1 AND J < 100
  811. Abbreviation:IF J > 1 AND < 100
  812.  
  813. IF line:IF K < -3 OR K = 6 OR K = 9 OR K = 12 OR K > 50
  814. Abbreviation:IF K < -3 OR = 6 OR 9 OR 12 OR > 50
  815.   You can say this:
  816.         IF AGE < 13
  817.                 DISPLAY "CHILD"
  818.         ELSE IF AGE < 20
  819.                 DISPLAY "TEENAGER"
  820.         ELSE IF AGE < 40
  821.                 DISPLAY "YOUNG ADULT"
  822.         ELSE IF AGE < 60
  823.                 DISPLAY "MIDDLE-AGED"
  824.         ELSE
  825.                 DISPLAY "SENIOR CITIZEN".
  826. It means, ``If AGE is less than 13, display the word CHILD; if 
  827. not less than 13, do the following: if AGE is less than 20, 
  828. display the word TEENAGER; if not less than 20, do the following: 
  829. if AGE is less than 40, . . . '' and so on. The computer will 
  830. display just one phrase, to describe the person's AGE.
  831.  
  832.                       UNTIL
  833.   You can say UNTIL:
  834. DATA DIVISION.
  835. WORKING-STORAGE SECTION.
  836. 01      I PIC 999.
  837.  
  838. PROCEDURE DIVISION.
  839. MAIN-ROUTINE.
  840.         MOVE 5 TO I.
  841.         PERFORM FIDDLE-WITH-I UNTIL I > 100.
  842.         STOP RUN.
  843. FIDDLE-WITH-I.
  844.         DISPLAY I.
  845.         COMPUTE I = I * 2.
  846. The computer will perform FIDDLE-WITH-I repeatedly, until I > 
  847. 100. The computer will display 5, 10, 20, 40, and 80. It will not 
  848. display 160.
  849.   Here are the details. When the computer encounters PERFORM 
  850. FIDDLE-WITH-I UNTIL I > 100, it checks whether I > 100. If I > 
  851. 100, the computer proceeds to the next statement (the STOP RUN); 
  852. but if I is not greater than 100, the computer performs 
  853. FIDDLE-WITH-I and then re-executes the statement PERFORM 
  854. FIDDLE-WITH-I UNTIL I > 100.
  855.   To translate a BASIC ``FOR...NEXT loop'' into COBOL, say 
  856. ``PERFORM'':
  857. BASIC           COBOL
  858. 10 FOR I = 5 TO 17PROCEDURE DIVISION.
  859. 20     PRINT I  MAIN-ROUTINE.
  860. 30 NEXT I               PERFORM DISPLAY-IT
  861.                                 VARYING I FROM 5 BY 1 UNTIL I > 
  862. 17.
  863.                         STOP RUN.
  864.                 DISPLAY-IT.
  865.                         DISPLAY I.
  866.  
  867. 10 FOR I = 5 TO 17 STEP 3PROCEDURE DIVISION.
  868. 20     PRINT I  MAIN-ROUTINE.
  869. 30 NEXT I               PERFORM DISPLAY-IT
  870.                                 VARYING I FROM 5 BY 3 UNTIL I > 
  871. 17.
  872.                         STOP RUN.
  873.                 DISPLAY-IT.
  874.                         DISPLAY I.
  875.  
  876. 10 FOR I = 5 TO 17PROCEDURE DIVISION.
  877. 20     FOR J = 1 TO 3MAIN-ROUTINE.
  878. 30         PRINT I,J        PERFORM DISPLAY-IT
  879. 40     NEXT J                   VARYING I FROM 5 BY 1 UNTIL I > 
  880. 17
  881. 50 NEXT I                       AFTER J FROM 1 BY 1 UNTIL J > 3.
  882.                         STOP RUN.
  883.                 DISPLAY-IT.
  884.                         DISPLAY I J.
  885.  
  886.                       GO TO
  887.   Like other computer languages, COBOL lets you say GO TO. In 
  888. COBOL, put a space between GO and TO. (In PASCAL, you do not put 
  889. a space between GO and TO.)
  890.   For example, instead of saying STOP RUN, you can say GO TO 
  891. MAIN-ROUTINE:
  892. PROCEDURE DIVISION.
  893. MAIN-ROUTINE.
  894.         DISPLAY "WHEATIES".
  895.         DISPLAY "ARE WONDERFUL".
  896.         GO TO MAIN-ROUTINE.
  897. The computer will display ``WHEATIES'' and ``ARE WONDERFUL'', 
  898. repeatedly:
  899. WHEATIES
  900. ARE WONDERFUL
  901. WHEATIES
  902. ARE WONDERFUL
  903. WHEATIES
  904. ARE WONDERFUL
  905. etc.
  906.  
  907.  
  908.   The main routine can consist of two paragraphs, called 
  909. MAIN-ROUTINE-BEGINNING and MAIN-ROUTINE-LOOP:
  910. PROCEDURE DIVISION.
  911. MAIN-ROUTINE-BEGINNING.
  912.         DISPLAY "PLEASE".
  913. MAIN-ROUTINE-LOOP.
  914.         DISPLAY "KISS".
  915.         DISPLAY "ME".
  916.         GO TO MAIN-ROUTINE-LOOP.
  917. The computer will display PLEASE, then repeatedly display KISS 
  918. and ME:
  919. PLEASE
  920. KISS
  921. ME
  922. KISS
  923. ME
  924. KISS
  925. ME
  926. etc.
  927.   The main routine can consist of three paragraphs, called 
  928. MAIN-ROUTINE-BEGINNING, MAIN-ROUTINE-LOOP, and 
  929. MAIN-ROUTINE-ENDING:
  930. DATA DIVISION.
  931. WORKING-STORAGE SECTION.
  932. 01      HUMAN-RESPONSE PIC XXX.
  933.  
  934. PROCEDURE DIVISION.
  935. MAIN-ROUTINE-BEGINNING.
  936.         DISPLAY "I WILL RECITE A SHORT POEM".
  937. MAIN-ROUTINE-LOOP.
  938.         DISPLAY " ".
  939.         DISPLAY "YOUR NOSE".
  940.         DISPLAY "BLOWS".
  941.         DISPLAY " ".
  942.         DISPLAY "WOULD YOU LIKE TO HEAR THE POEM AGAIN?".
  943.         ACCEPT HUMAN-RESPONSE.
  944.         IF HUMAN-RESPONSE = "YES"
  945.                 GO TO MAIN-ROUTINE=LOOP.
  946. MAIN-ROUTINE-ENDING.
  947.         DISPLAY "YOU HAVE BEEN A GREAT AUDIENCE".
  948.         STOP RUN.
  949.   When you run that program, the computer says:
  950. I WILL RECITE A SHORT POEM
  951. Then it recites the program:
  952. YOUR NOSE
  953. BLOWS
  954. Then it asks:
  955. WOULD YOU LIKE TO HEAR THE POEM AGAIN?
  956.   If you answer YES, the computer repeats the poem, then asks 
  957. whether you'd like to hear it a third time. If you answer YES 
  958. again, the computer recites the poem a third time, then asks 
  959. whether you'd like to hear it a fourth time. The computer recites 
  960. the poem repeatedly, until you finally stop answering YES. Then 
  961. the computer says ___ 
  962. YOU HAVE BEEN A GREAT AUDIENCE
  963. and stops.
  964.                                          In that program, if you 
  965. don't answer YES, the computer doesn't repeat the poem. So if you 
  966. don't answer YES, the computer acts as if you said NO. The 
  967. following version is an improvement; if you don't answer YES, and 
  968. you don't say NO, the computer asks the question again:
  969. DATA DIVISION.
  970. WORKING-STORAGE SECTION.
  971. 01      HUMAN-RESPONSE PIC XXX.
  972.  
  973. PROCEDURE DIVISION.
  974. MAIN-ROUTINE-BEGINNING.
  975.         DISPLAY "I WILL RECITE A SHORT POEM".
  976. MAIN-ROUTINE-LOOP.
  977.         DISPLAY " ".
  978.         DISPLAY "YOUR NOSE".
  979.         DISPLAY "BLOWS".
  980.         DISPLAY " ".
  981.         PERFORM GET-HUMAN-RESPONSE.
  982.         IF HUMAN-RESPONSE = "YES"
  983.                 GO TO MAIN-ROUTINE-LOOP.
  984. MAIN-ROUTINE-ENDING.
  985.         DISPLAY "YOU HAVE BEEN A GREAT AUDIENCE".
  986.         STOP RUN.
  987. GET-HUMAN-RESPONSE.                                      
  988.         DISPLAY "WOULD YOU LIKE TO HEAR THE POEM AGAIN?".
  989.         ACCEPT HUMAN-RESPONSE.                           
  990.         IF HUMAN-RESPONSE NOT = "YES" AND NOT = "NO"     
  991.                 DISPLAY "PLEASE SAY YES OR NO!"          
  992.                 GO TO GET-HUMAN-RESPONSE.                
  993.                                          GO TO resembles PERFORM. 
  994. Here's the difference between GO TO and PERFORM. . . .
  995. To go to a different routine, say PERFORM.
  996. To go to a different paragraph in the same routine, say GO TO.
  997.                                          For example, suppose you 
  998. want to go from MAIN-ROUTINE-BEGINNING to FUNNY-SUBROUTINE; since 
  999. you're going to a different routine, say PERFORM.
  1000.                                          Suppose you want to go 
  1001. from MAIN-ROUTINE-BEGINNING to MAIN-ROUTINE-ENDING; since you're 
  1002. going to a different paragraph in the same routine, say GO TO.
  1003.  
  1004.                                                        THRU
  1005.                                          Like the main routine, a 
  1006. subroutine can consist of several paragraphs. For example, 
  1007. subroutine FUNNY-FACE can consist of three paragraphs, called 
  1008. FUNNY-FACE-BEGINNING, FUNNY-FACE-LOOP, and FUNNY-FACE-ENDING. To 
  1009. make the computer do the entire subroutine, say:
  1010.         PERFORM FUNNY-FACE-BEGINNING THRU FUNNY-FACE-ENDING.
  1011.  
  1012.  
  1013.              DATA FILES
  1014.   To manipulate a data file whose name is POEM, fill in the 
  1015. blanks:
  1016. IDENTIFICATION DIVISION.
  1017. PROGRAM-ID.
  1018.         The program's name.
  1019. AUTHOR
  1020.         Your name.
  1021.  
  1022. ENVIRONMENT DIVISION.
  1023. CONFIGURATION SECTION.
  1024. SOURCE-COMPUTER.
  1025.         The computer's name.
  1026. OBJECT-COMPUTER.
  1027.         The computer's name again.
  1028. INPUT-OUTPUT SECTION.
  1029. FILE-CONTROL.
  1030.         SELECT POEM-FILE ASSIGN TO the file's location.
  1031.  
  1032. DATA DIVISION.
  1033. FILE SECTION.
  1034. FD      POEM-FILE how the file is labeled.
  1035. 01      POEM-LINE PIC a picture of a line of the file.
  1036. WORKING-STORAGE SECTION.
  1037. A description of each variable that's not in the file.
  1038.  
  1039. PROCEDURE DIVISION.
  1040. MAIN-ROUTINE.
  1041.         OPEN output or input POEM-FILE.
  1042.         What you want to do to the file.
  1043.         CLOSE POEM-FILE.
  1044.         STOP RUN.
  1045.  
  1046.          The four divisions
  1047.   Like every COBOL program, that outline consists of four 
  1048. divisions: the IDENTIFICATION DIVISION, the ENVIRONMENT DIVISION, 
  1049. the DATA DIVISION, and the PROCEDURE DIVISION. Let's look at each 
  1050. division.
  1051.   IDENTIFICATION DIVISION The IDENTIFICATION DIVISION consists of 
  1052. two paragraphs: the PROGRAM-ID and the AUTHOR. For the 
  1053. PROGRAM-ID, fill in the program's name, which must be different 
  1054. from the name of the file. Since the name of the file is POEM, 
  1055. the name of the program must not be POEM. If you're lazy, you can 
  1056. omit the AUTHOR.
  1057.   ENVIRONMENT DIVISION The ENVIRONMENT DIVISION consists of two 
  1058. sections: the CONFIGURATION SECTION and the INPUT-OUTPUT SECTION.
  1059.   The CONFIGURATION SECTION consists of two paragraphs: the 
  1060. SOURCE-COMPUTER and the OBJECT-COMPUTER. On CDC and PDP-11 
  1061. computers, the CONFIGURATION SECTION is required; but on IBM, 
  1062. Eclipse, PDP-10, and PDP-20 computers, the entire CONFIGURATION 
  1063. SECTION is optional, so you can abridge the ENVIRONMENT DIVISION:
  1064. ENVIRONMENT DIVISION.
  1065. INPUT-OUTPUT SECTION.
  1066. FILE-CONTROL.
  1067.         SELECT POEM-FILE ASSIGN TO the file's location.
  1068.   The INPUT-OUTPUT SECTION consists of just one paragraph, which 
  1069. is the FILE-CONTROL. The FILE-CONTROL paragraph consists of a 
  1070. sentence that says SELECT, then the file's name (POEM-FILE), then 
  1071. ASSIGN TO, and finally a blank (which you must fill in, and which 
  1072. tells the file's location).
  1073.  
  1074.   What do you put in that blank? The answer depends on the file's 
  1075. location. Is the file on a disk? On punched cards? Or on paper 
  1076. produced by the printer? Here's what to put in the blank, for 
  1077. various computers:
  1078.           Printer   Card reader Disk
  1079. Eclipse   PRINTER   "$CDR"      "POEM"
  1080. CDC       OUTPUT    INPUT       POEM
  1081. PDP-11    "LP:"     "CR:"       "DK":
  1082. PDP-10, PDP-20LPT   CDR         DSK RECORDING MODE ASCII
  1083. IBM using OSUT-S-POEMUT-S-POEM  UT-S-POEM
  1084. IBM using DOSSYS006-UR-1403-S SYS005-UR-2540R-SSYS020-UT-3330-S-P
  1085. OEM
  1086.   DATA DIVISION The DATA DIVISION consists of two sections: the 
  1087. FILE SECTION and the WORKING-STORAGE SECTION.
  1088.   At the beginning of the FILE SECTION, say FD (which means 
  1089. ``File Description''). To the right of the FD, say POEM-FILE, and 
  1090. then fill in the blank, which tells how the file is labeled:
  1091. Computer          What to put in the blank
  1092. CDC               LABEL RECORDS ARE OMITTED
  1093. PDP-11: disk      LABEL RECORDS ARE STANDARD VALUE OF ID "POEM"
  1094. PDP-11: printer or card readerLABEL RECORDS ARE OMITTED
  1095. PDP-10, PDP-20    VALUE ID "POEM■■■■■"
  1096. IBM OS            LABEL RECORDS ARE STANDARD
  1097. IBM DOS: disk     LABEL RECORDS ARE STANDARD
  1098. IBM DOS: printer or card readerLABEL RECORDS ARE OMITTED
  1099.   On PDP-10 and PDP-20 computers, put enough blank spaces (■) 
  1100. after POEM so that the string has 9 characters. On Eclipse 
  1101. computers, do not fill in the blank; just say:
  1102. FD      POEM-FILE.
  1103.   Underneath the line that says FD, you must say 01. The 01 line 
  1104. includes a picture of a line of the file. For example, if a line 
  1105. of the file is an 80-character string, the 01 line should say:
  1106. 01      POEM-LINE PIC X(80).
  1107.   The WORKING-STORAGE SECTION describes each variable that's not 
  1108. in the file.
  1109.   PROCEDURE DIVISION The PROCEDURE DIVISION's MAIN-ROUTINE should 
  1110. begin with the word OPEN, and end with the words CLOSE and STOP 
  1111. RUN.
  1112.   In the OPEN statement, you can say either ___ 
  1113.         OPEN OUTPUT POEM-FILE.
  1114. or:
  1115.         OPEN INPUT POEM-FILE.
  1116. If you say OPEN OUTPUT POEM-FILE, the computer will output to the 
  1117. POEM-FILE; so it will copy information from the RAM to the 
  1118. POEM-FILE. If you say OPEN INPUT POEM-FILE, the computer will 
  1119. input from the POEM-FILE; so it will copy information from the 
  1120. POEM-FILE to the RAM.
  1121.   In the PROCEDURE DIVISION, when you fill in the blank about 
  1122. ``what you want to do to the file'', you must say either WRITE 
  1123. POEM-LINE or READ POEM-FILE. If the file is OPEN OUTPUT (which 
  1124. means you're copying from the RAM to the file), say WRITE 
  1125. POEM-LINE; if the file is OPEN INPUT (which means you're copying 
  1126. from the file to the RAM), say READ POEM-FILE.
  1127.  
  1128.                      Writing
  1129.   Here's a poetic masterpiece:
  1130. CANDY IS DANDY
  1131. BUT LIKKER IS QUIKKER
  1132. It was composed by the famous poet Ogden Nash.
  1133.   This program makes the computer write that masterpiece onto a 
  1134. disk, and make the masterpiece become a file named POEM:
  1135. Program                           Meaning
  1136. IDENTIFICATION DIVISION.
  1137. PROGRAM-ID.
  1138.         CANDY.                    This program is named CANDY.
  1139.  
  1140. ENVIRONMENT DIVISION.
  1141. CONFIGURATION SECTION.
  1142. SOURCE-COMPUTER.
  1143.         The computer's name.
  1144. OBJECT-COMPUTER.
  1145.         The computer's name again.
  1146. INPUT-OUTPUT SECTION.
  1147. FILE-CONTROL.
  1148.         SELECT POEM-FILE ASSIGN TO the file's location.This 
  1149. program uses a file called POEM.
  1150.  
  1151. DATA DIVISION.
  1152. FILE SECTION.
  1153. FD      POEM-FILE how the file is labeled.
  1154. 01      POEM-LINE PIC X(21).      Make each line have 21 
  1155. characters, like this:
  1156.                                   CANDY IS DANDY■■■■■■■
  1157. PROCEDURE DIVISION.               BUT LIKKER IS QUIKKER
  1158. MAIN-ROUTINE.
  1159.         OPEN OUTPUT POEM-FILE.    Prepare to output to POEM-FILE.
  1160.         MOVE "CANDY IS DANDY" TO POEM-LINE.Make POEM-LINE be 
  1161. ``CANDY IS DANDY■■■■■■■''.
  1162.         WRITE POEM-LINE.          Copy that POEM-LINE to the 
  1163. file.
  1164.         MOVE "BUT LIKKER IS QUIKKER" TO POEM-LINE.Make POEM-LINE 
  1165. become this new string: ``BUT LIKKER IS QUIKKER''.
  1166.         WRITE POEM-LINE.          Copy that new POEM-LINE to the 
  1167. file.
  1168.         CLOSE POEM-FILE.          Finish using POEM-FILE.
  1169.         STOP RUN.                 Stop running this program.
  1170.   That program doesn't require a WORKING-STORAGE SECTION, so I 
  1171. omitted it. Since I was lazy, I also omitted the AUTHOR 
  1172. paragraph.
  1173.   When you run that program, the computer will create a file on 
  1174. disk. The file will be called POEM. It will contain this message:
  1175. CANDY IS DANDY■■■■■■■
  1176. BUT LIKKER IS QUIKKER
  1177.  
  1178.                      Reading
  1179.   This program reads the file POEM, and displays it on your 
  1180. terminal:
  1181. IDENTIFICATION DIVISION.
  1182. PROGRAM-ID.
  1183.         READER.
  1184.  
  1185. The ENVIRONMENT DIVISION and DATA DIVISION are the same as the 
  1186. previous program's.
  1187.  
  1188. PROCEDURE DIVISION.
  1189. MAIN-ROUTINE-BEGINNING.
  1190.         OPEN INPUT POEM-FILE.       Find POEM on the disk, and 
  1191. prepare to input from it.
  1192. MAIN-ROUTINE-LOOP.
  1193.         READ POEM-FILE AT END GO TO MAIN-ROUTINE-ENDING.Read a 
  1194. line from POEM-FILE; if no more lines, go to next paragraph.
  1195.         DISPLAY POEM-LINE.          Display that line, so you see 
  1196. it on your screen.
  1197.         GO TO MAIN-ROUTINE-LOOP.    Go back to read another line.
  1198. MAIN-ROUTINE-ENDING.
  1199.         DISPLAY "THAT WAS THE WHOLE POEM".Display ``THAT WAS THE 
  1200. WHOLE POEM'' on your screen.
  1201.         CLOSE POEM-FILE.            Finish using POEM-FILE.
  1202.         STOP RUN.                   Stop running this program.
  1203.   In the MAIN-ROUTINE-LOOP, the first line means: try to READ a 
  1204. line from POEM-FILE; but if a line cannot be read (because the 
  1205. file has ended), go to MAIN-ROUTINE-ENDING instead.
  1206.   The READ statement differs from the WRITE statement in two 
  1207. ways:
  1208. A WRITE statement mentions a LINE, but a READ statement mentions 
  1209. a FILE.
  1210. A READ statement must contain the words AT END.
  1211.  
  1212.  
  1213.                     Counting
  1214.   This program reads a file called POEM, counts how many lines 
  1215. are in it, and displays the count:
  1216. IDENTIFICATION DIVISION.
  1217. PROGRAM-ID.
  1218.         COUNTS.
  1219.  
  1220. The ENVIRONMENT DIVISION is same as previous program's.
  1221.  
  1222. DATA DIVISION.
  1223. FILE SECTION.
  1224. FD      POEM-FILE how the file is labeled.
  1225. 01      POEM-LINE PIC X(21).        Assume each POEM-LINE has 21 
  1226. characters.
  1227. WORKING-STORAGE SECTION.
  1228. 01      COUNT-OF-HOW-MANY-LINES PIC 99.Assumes the count is a 
  1229. two-digit number,
  1230.                                     so assume POEM has less than 
  1231. 100 lines.
  1232. PROCEDURE DIVISION.
  1233. MAIN-ROUTINE-BEGINNING.
  1234.         OPEN INPUT POEM-FILE.       Find POEM on the disk.
  1235.         MOVE 0 TO COUNT-OF-HOW-MANY-LINES.Start the count at 0.
  1236. MAIN-ROUTINE-LOOP.
  1237.         READ POEM-FILE AT END GO TO MAIN-ROUTINE-ENDING.Read a 
  1238. line from POEM-FILE.
  1239.         ADD 1 TO COUNT-OF-HOW-MANY-LINES.Add 1 to the count.
  1240.         GO TO MAIN-ROUTINE-LOOP.    Go read another line.
  1241. MAIN-ROUTINE-ENDING.                When all lines have been 
  1242. read,
  1243.         DISPLAY COUNT-OF-HOW-MANY-LINES.display the count,
  1244.         CLOSE POEM-FILE.            finish using POEM-FILE,
  1245.         STOP RUN.                   and stop running this 
  1246. program.
  1247.  
  1248.                      Copying
  1249.   This program reads a file called POEM, and creates a copy of 
  1250. it; the copy is a file called POEM2:
  1251. IDENTIFICATION DIVISION.
  1252. PROGRAM-ID.
  1253.         COPIER.
  1254.  
  1255. ENVIRONMENT DIVISION.
  1256. CONFIGURATION SECTION.
  1257. SOURCE-COMPUTER.
  1258.         The computer's name.
  1259. OBJECT-COMPUTER.
  1260.         The computer's name again.
  1261. INPUT-OUTPUT SECTION.
  1262. FILE-CONTROL.
  1263.         SELECT POEM-FILE ASSIGN TO the location of POEM.
  1264.         SELECT POEM2-FILE ASSIGN TO the location of POEM-2.
  1265.  
  1266. DATA DIVISION.
  1267. FILE SECTION.
  1268. FD      POEM-FILE the labeling for POEM.
  1269. 01      POEM-LINE PIC X(21).
  1270. FD      POEM2-FILE the labeling for POEM2.
  1271. 01      POEM2-LINE PIC X(21).
  1272.  
  1273. PROCEDURE DIVISION.
  1274. MAIN-ROUTINE-BEGINNING.
  1275.         OPEN INPUT POEM-FILE.       Prepare to input from 
  1276. POEM-FILE,
  1277.         OPEN OUTPUT POEM2-FILE.     and output to POEM2-FILE.
  1278. MAIN-ROUTINE-LOOP.                  Do the following repeatedly:
  1279.         READ POEM-FILE AT END GO TO MAIN-ROUTINE-ENDING.read a 
  1280. line from POEM-FILE,
  1281.         MOVE POEM-LINE TO POEM2-LINE.copy that line to 
  1282. POEM2-LINE,
  1283.         WRITE POEM2-LINE.           and write POEM2-LINE to 
  1284. POEM2-FILE.
  1285.         GO TO MAIN-ROUTINE-LOOP.
  1286. MAIN-ROUTINE-ENDING.                At the end,
  1287.         DISPLAY "THE FILE HAS BEEN COPIED".display ``THE FILE HAS 
  1288. BEEN COPIED'' on the screen,
  1289.         CLOSE POEM-FILE POEM2-FILE. finish using the files,
  1290.         STOP RUN.                   and stop running this 
  1291. program.
  1292.  
  1293.  
  1294.                     Pictures
  1295.   Suppose you're dealing with a file named JOE, and each line of 
  1296. JOE-FILE is a three-digit number. Should the line's picture be 
  1297. edited (JOE-LINE PIC ZZZ) or unedited (JOE-LINE PIC 999)?
  1298.   When you read a file, the line's picture must be unedited and 
  1299. match the picture in the program that wrote the file.
  1300.   When you write a file, ask yourself, ``What will read it?'' If 
  1301. the answer is ``a COBOL program'', the picture must be unedited. 
  1302. If the answer is ``only a human'', edit the picture.
  1303.   Remember: if one program writes a file, and another program 
  1304. reads it, both programs must use the same picture. For example, 
  1305. if a program writes JACK-FILE and says JACK-LINE PIC S9999V99, 
  1306. the program that reads JACK-FILE must also say JACK-LINE PIC 
  1307. S9999V99.
  1308.  
  1309.                   Peculiarities
  1310.   To write and read unedited numbers, the computer takes a 
  1311. short-cut: it omits decimal points, and locates the negative sign 
  1312. on top of the last digit. For example, instead of writing 
  1313. -0034.27 in JACK-FILE, the computer writes just 003427. When 
  1314. another program reads 003427 from the file, the S9999V99 picture 
  1315. tells the computer the 003427 means -0034.27.
  1316.   After you WRITE a line, you cannot use the line again in the 
  1317. program. For example, after you say WRITE POEM-LINE, you should 
  1318. not say MOVE POEM-LINE TO K; it won't work.
  1319.  
  1320.                  Multiple widths
  1321.   Let's make the computer compute the square of 12 and the square 
  1322. of 13 and write this file:
  1323. HERE ARE THE SQUARES:
  1324. 144
  1325. 169
  1326. THEY WERE REAL GROOVY
  1327. The top and bottom lines are long strings whose pictures are 
  1328. X(21). The other two lines are short numbers whose pictures are 
  1329. 999.
  1330.   Here's the program:
  1331. IDENTIFICATION DIVISION.
  1332. PROGRAM-ID.
  1333.         SQUARE.
  1334.  
  1335. ENVIRONMENT DIVISION.
  1336. CONFIGURATION SECTION.
  1337. SOURCE-COMPUTER.
  1338.         The computer's name.
  1339. OBJECT-COMPUTER.
  1340.         The computer's name again.
  1341. INPUT-OUTPUT SECTION.
  1342. FILE-CONTROL.
  1343.         SELECT REPORT-FILE ASSIGN TO location of REPORT.This 
  1344. program uses a file called REPORT.
  1345.  
  1346. DATA DIVISION.
  1347. FILE SECTION.
  1348. FD      REPORT-FILE the labeling for REPORT.
  1349. 01      REPORT-LINE PIC X(21).      REPORT-LINE is a 21-character 
  1350. string.
  1351. 01      REPORT-LINE2 PIC 999.       REPORT-LINE2 is a 3-digit 
  1352. number.
  1353.  
  1354. PROCEDURE DIVISION.
  1355. MAIN-ROUTINE.
  1356.         OPEN OUTPUT REPORT-FILE.    Create a file named REPORT.
  1357.         MOVE "HERE ARE THE SQUARES:" TO REPORT-LINE.REPORT-LINE 
  1358. is ``HERE ARE THE SQUARES:''.
  1359.         WRITE REPORT-LINE.          Write ``HERE ARE THE 
  1360. SQUARES:''.
  1361.         COMPUTE REPORT-LINE2 = 12 * 12.REPORT-LINE2 is 144.
  1362.         WRITE REPORT-LINE2.         Write 144.
  1363.         COMPUTE REPORT-LINE2 = 13 * 13.REPORT-LINE2 is 169.
  1364.         WRITE REPORT-LINE2.         Write 169.
  1365.         MOVE "THEY WERE REAL GROOVY" TO REPORT-LINE.REPORT-LINE 
  1366. is ``THEY WERE REAL GROOVY''.
  1367.         WRITE REPORT-LINE.          Write ``THEY WERE REAL 
  1368. GROOVY''.
  1369.         CLOSE REPORT-FILE.          Finish using REPORT.
  1370.         STOP RUN.                   Stop running this program.
  1371.  
  1372.  
  1373.  
  1374.                ADVANCED STRUCTURES
  1375.   COBOL lets you create and manipulate advanced structures.
  1376.  
  1377.                    Group items
  1378.   In the data division, you can say:
  1379. 01      K.
  1380.         02      L PIC 999.
  1381.         02      M PIC 9.
  1382.         02      N PIC 99.
  1383. That means K is a combination of L, M, and N. If the procedure 
  1384. division says ___ 
  1385.         MOVE 427 TO L.
  1386.         MOVE 8 TO M.
  1387.         MOVE 31 TO N.
  1388. then K will be ``427831''.
  1389.   Since K is a combination of other variables, K is called a 
  1390. group variable or group item. L, M, and N are elementary items. 
  1391. Notice that K is the string ``427831'', not the number 427831. A 
  1392. group item is always a string. Since K is a string, not a number, 
  1393. you cannot say ADD 1 TO K, although you can say ADD 1 TO L or ADD 
  1394. 1 TO M or ADD 1 TO N.
  1395.   Here's a group item, for a weight-reducing studio:
  1396. 01      PERSONAL-INFO-ABOUT-CLIENT.
  1397.         02      CLIENT-NAME.
  1398.                 03      FIRST-NAME PIC X(15).
  1399.                 03      MIDDLE-INITIAL PIC X.
  1400.                 03      LAST-NAME PIC X(20).
  1401.         02      CLIENT-SEX PIC X.A person's sex is ``M'' or 
  1402. ``F''.
  1403.         02      CLIENT-AGE PIC 99.
  1404.         02      WEIGHT-PROGRESS.
  1405.                 03      WEIGHT-WHEN-ENTERED-PROGRAM PIC 999.
  1406.                 03      WEIGHT-THIS-WEEK PIC 999.
  1407.                 03      NUMBER-OF-WEEKS-SO-FAR PIC 999.
  1408.   PERSONAL-INFO-ABOUT-CLIENT is composed of CLIENT-NAME (which is 
  1409. composed of FIRST-NAME, MIDDLE-INITIAL, and LAST-NAME), 
  1410. CLIENT-SEX, and WEIGHT-PROGRESS (which is composed of 
  1411. WEIGHT-WHEN-ENTERED-PROGRAM, WEIGHT-THIS-WEEK, and 
  1412. NUMBER-OF-WEEKS-SO-FAR). So PERSONAL-INFO-ABOUT-CLIENT is 
  1413. composed of numbers and strings.
  1414.   Altogether, PERSONAL-INFO-ABOUT-CLIENT contains 48 characters 
  1415. (15 + 1 + 20 + 1 + 2 + 3 + 3 + 3). The computer considers 
  1416. PERSONAL-INFO-ABOUT-CLIENT to be a string whose picture is X(48).
  1417.   If you say L PIC X(48), you can move all the 
  1418. PERSONAL-INFO-ABOUT-CLIENT to L by saying:
  1419.         MOVE PERSONAL-INFO-ABOUT-CLIENT TO L.
  1420.   To move the CLIENT-NAME to M, without moving the CLIENT-SEX, 
  1421. CLIENT-AGE, and WEIGHT-PROGRESS, say:
  1422.         MOVE CLIENT-NAME TO M.
  1423.   To write lots of information to a file, make the file's LINE be 
  1424. a group item.
  1425.  
  1426.   How to extract from a file Suppose you've already created a 
  1427. file whose name is EMPLOY; it's on disk or cards. Suppose the 
  1428. file contains information about employees. Suppose each line of 
  1429. the file contains 80 characters, as follows. Characters 1-40 are 
  1430. the employee's name. Characters 61-70 are the employee's home 
  1431. phone number, including the area code. The other characters 
  1432. (41-60 and 71-80) are miscellaneous information (such as the 
  1433. employee's age, sex, address, salary, kind of job, and number of 
  1434. years with the company).
  1435.   Let's create a new file, called REPORT, on disk or on the 
  1436. printer's paper. Let's make REPORT contain just the employees' 
  1437. names and phone numbers, and omit the ``miscellaneous 
  1438. information''. Here's how:
  1439. IDENTIFICATION DIVISION.
  1440. PROGRAM-ID.
  1441.         PHONES.
  1442.  
  1443. ENVIRONMENT DIVISION.
  1444. CONFIGURATION SECTION.
  1445. SOURCE-COMPUTER.
  1446.         The computer's name.
  1447. OBJECT-COMPUTER.
  1448.         The computer's name again.
  1449. INPUT-OUTPUT SECTION.
  1450. FILE-CONTROL.
  1451.         SELECT EMPLOY-FILE ASSIGN TO location of EMPLOY.
  1452.         SELECT REPORT-FILE ASSIGN TO location of REPORT.
  1453.  
  1454. DATA DIVISION.                      Throughout the DATA DIVISION, 
  1455. the special word "FILLER"
  1456. FILE SECTION.                       stands for data the program 
  1457. won't use.
  1458. FD      EMPLOY-FILE the labeling for EMPLOY.
  1459. 01      EMPLOY-LINE.
  1460.         02      EMPLOYEE-NAME PIC X(40).Characters 1-40 are 
  1461. EMPLOYEE-NAME.
  1462.         02      FILLER PIC X(20).   Characters 41-60 are 
  1463. irrelevant.
  1464.         02      HOME-PHONE.
  1465.                 03      AREA-CODE PIC 999.Characters 61-63 are 
  1466. AREA-CODE.
  1467.                 03      PHONE-EXCHANGE PIC 999.Characters 64-66 
  1468. are PHONE-EXCHANGE.
  1469.                 03      REST-OF-PHONE-NUMBER PIC 9999.Characters 
  1470. 67-70 are REST-OF-PHONE-NUMBER.
  1471.         02      FILLER PIC X(10).   Characters 71-80 are 
  1472. irrelevant.
  1473. FD      REPORT-FILE the labeling for REPORT.
  1474. 01      REPORT-LINE.                The PICs say HOME-PHONE looks 
  1475. like this ___ 
  1476.         02      EMPLOYEE-NAME-REPORTED PIC X(40).6176662666
  1477.         02      HOME-PHONE-REPORTED.but make HOME-PHONE-REPORTED 
  1478. look like this:
  1479.                 03      LEFT-PARENTHESIS PIC X.(617) 666-2666
  1480.                 03      AREA-CODE-REPORTED PIC 999.
  1481.                 03      RIGHT-PARENTHESIS PIC X.
  1482.                 03      PHONE-EXCHANGE-REPORTED PIC B999.
  1483.                 03      THE-DASH PIC X.
  1484.                 03      REST-OF-PHONE-NUMBER-REPORTED PIC 9999.
  1485.  
  1486. PROCEDURE DIVISION.
  1487. MAIN-ROUTINE-BEGINNING.
  1488.         OPEN INPUT EMPLOY-FILE.
  1489.         OPEN OUTPUT REPORT-FILE.
  1490. MAIN-ROUTINE-LOOP.                  The MAIN-ROUTINE-LOOP
  1491.         READ EMPLOY-FILE AT END GO TO MAIN-ROUTINE-ENDING.reads a 
  1492. line from EMPLOY-FILE,
  1493.         MOVE EMPLOYEE-NAME TO EMPLOYEE-NAME-REPORTED.copies data 
  1494. into each part of REPORT-LINE,
  1495.         MOVE "(" TO LEFT-PARENTHESIS.
  1496.         MOVE AREA-CODE TO AREA-CODE-REPORTED.
  1497.         MOVE ")" TO RIGHT-PARENTHESIS.
  1498.         MOVE PHONE-EXCHANGE TO PHONE-EXCHANGE-REPORTED.
  1499.         MOVE "-" TO THE-DASH.
  1500.         MOVE REST-OF-PHONE-NUMBER TO 
  1501. REST-OF-PHONE-NUMBER-REPORTED.
  1502.         WRITE REPORT-LINE.          and then writes REPORT-LINE.
  1503.         GO TO MAIN-ROUTINE-LOOP.
  1504. MAIN-ROUTINE-ENDING.
  1505.         CLOSE EMPLOY-FILE REPORT-FILE.
  1506.         STOP RUN.
  1507.  
  1508.  
  1509.                 SORT
  1510.   Suppose CUSTOM is a disk file that contains information about 
  1511. your customers. Suppose each line of the file contains 80 
  1512. characters, as follows. . . . 
  1513. Characters  1-20: the customer's last name
  1514. Characters 21-80: other information about the customer
  1515.   Alphabetical order Here's how to put the file in alphabetical 
  1516. order, according to the customer's name:
  1517. IDENTIFICATION DIVISION.
  1518. PROGRAM-ID.
  1519.         ALPHA.
  1520.  
  1521. ENVIRONMENT DIVISION.
  1522. CONFIGURATION SECTION.
  1523. SOURCE-COMPUTER.
  1524.         The computer's name.
  1525. OBJECT-COMPUTER.
  1526.         The computer's name again.
  1527. INPUT-OUTPUT SECTION.
  1528. FILE-CONTROL.
  1529.         SELECT CUSTOM-FILE ASSIGN TO the location of CUSTOM.
  1530.         SELECT SORT-FILE ASSIGN TO the location of SORT.
  1531.  
  1532. DATA DIVISION.
  1533. FILE SECTION.
  1534. FD      CUSTOM-FILE the labeling for CUSTOM.
  1535. 01      CUSTOM-LINE PIC X(80).
  1536. SD      SORT-FILE.
  1537. 01      SORT-LINE.
  1538.         02      LAST-NAME PIC X(20).
  1539.         02      FILLER PIC X(60).
  1540.  
  1541. PROCEDURE DIVISION.
  1542. MAIN-ROUTINE.
  1543.         SORT SORT-FILE
  1544.                 ASCENDING KEY LAST-NAME
  1545.                 USING CUSTOM-FILE
  1546.                 GIVING CUSTOM-FILE.
  1547.         STOP RUN.
  1548.   Putting a file in order, by alphabetizing or any other method, 
  1549. is called sorting. To sort the CUSTOM-FILE, the computer has to 
  1550. create a temporary disk file called a SORT-FILE.
  1551.   In the sentence that says SELECT SORT-FILE, here's what to put 
  1552. for ``the location of SORT'':
  1553. Computer  The location of SORT
  1554. Eclipse   "SORT"
  1555. CDC       SORT
  1556. PDP-10, PDP-20DSK DSK DSK RECORDING MODE ASCII
  1557. IBM using OSUT-S-POEM
  1558. IBM using DOSSYS001-UT-3330-S-SORTWK1
  1559.   In the DATA DIVISION's FILE SECTION, the SD means a Sort-file 
  1560. Description. In the PROCEDURE DIVISION, the SORT sentence makes 
  1561. the computer automatically open the CUSTOM-FILE, sort it, and 
  1562. close it.
  1563.   In the SORT sentence, if you replace ASCENDING by DESCENDING, 
  1564. the computer will sort the file in reverse order, so the Z's come 
  1565. first and the A's come last.
  1566.   You can make the program fancier, by inserting extra statements 
  1567. before and after the SORT statement. But since the SORT statement 
  1568. automatically tells the computer to open CUSTOM-FILE, the 
  1569. CUSTOM-FILE must not be open already. If you already said OPEN 
  1570. CUSTOM-FILE, you must say CLOSE CUSTOM-FILE before you give the 
  1571. SORT statement.
  1572.   If you replace GIVING CUSTOM-FILE by GIVING REPORT-FILE, the 
  1573. computer won't change CUSTOM-FILE, but will create a REPORT-FILE 
  1574. containing the information sorted. For REPORT-FILE, you must type 
  1575. an FD and SELECT it. The computer will automatically open it, so 
  1576. it
  1577. must not be open already.
  1578.                                          Who bought the most? 
  1579. Within each line of CUSTOM-FILE, suppose characters 51-57 tell 
  1580. how much the customer bought from you during the past year. Let's 
  1581. find out which customers bought the most.
  1582.                                          Let's make the computer 
  1583. print the customer that bought the most, then the customer that 
  1584. bought the next most, etc. If two customers bought exactly the 
  1585. same amount, let's make the computer print their names in 
  1586. alphabetical order.
  1587.                                          This program does it:
  1588. IDENTIFICATION DIVISION.
  1589. PROGRAM-ID.
  1590.         BIGBUY.
  1591.  
  1592. The ENVIRONMENT DIVISION is same as the previous program's.
  1593.  
  1594. DATA DIVISION.
  1595. FILE SECTION.
  1596. FD      CUSTOM-FILE the labeling for CUSTOM.
  1597. 01      CUSTOM-LINE PIC X(80).
  1598. SD      SORT-FILE.
  1599. 01      SORT-LINE.
  1600.         02      LAST-NAME PIC X(20).                               
  1601. characters 1-20
  1602.         02      FILLER PIC X(30).                                  
  1603. characters 21-50
  1604.         02      AMOUNT-BOUGHT-DURING-YEAR PIC 99999V99.
  1605.         02      FILLER PIC X(23).
  1606.  
  1607. PROCEDURE DIVISION.
  1608. MAIN-ROUTINE.
  1609.         SORT SORT-FILE
  1610.                 DESCENDING KEY AMOUNT-BOUGHT-DURING-YEAR
  1611.                 ASCENDING KEY LAST-NAME
  1612.                 USING CUSTOM-FILE
  1613.                 GIVING CUSTOM-FILE.
  1614.         STOP RUN.
  1615.                                          The SORT sentence says: 
  1616. sort the file so that AMOUNT-BOUGHT-DURING-YEAR is in DESCENDING 
  1617. order; in case of a tie, put LAST-NAME in ASCENDING order.
  1618.                       MERGE
  1619.   Suppose OLDCUS and NEWCUS are files: OLDCUS describes your old 
  1620. customers, and NEWCUS describes your newer customers. In those 
  1621. files, each line contains 80 characters; characters 1-20 contain 
  1622. the customer's last name. Each file's already in alphabetical 
  1623. order, by customer's last name.
  1624.   Let's combine the two files. In other words, let's create a 
  1625. ``combination'' file (on disk or printer paper), called ALLCUS, 
  1626. that contains all the customers; and let's make ALLCUS be in 
  1627. alphabetical order also. Here's how:
  1628. IDENTIFICATION DIVISION.
  1629. PROGRAM-ID.
  1630.         MERGER.
  1631.  
  1632. ENVIRONMENT DIVISION.
  1633. CONFIGURATION SECTION.
  1634. SOURCE-COMPUTER.
  1635.         The computer's name.
  1636. OBJECT-COMPUTER.
  1637.         The computer's name again.
  1638. INPUT-OUTPUT SECTION.
  1639. FILE-CONTROL.
  1640.         SELECT OLDCUS-FILE ASSIGN TO the location of OLDCUS.
  1641.         SELECT NEWCUS-FILE ASSIGN TO the location of NEWCUS.
  1642.         SELECT ALLCUS-FILE ASSIGN TO the location of ALLCUS.
  1643.         SELECT SORT-FILE ASSIGN TO the location of SORT.
  1644.  
  1645. DATA DIVISION.
  1646. FILE SECTION.
  1647. FD      OLDCUS-FILE the labeling for OLDCUS.
  1648. 01      OLDCUS-LINE PIC X(80).
  1649. FD      NEWCUS-FILE the labeling for NEWCUS.
  1650. 01      NEWCUS-LINE PIC X(80).
  1651. FD      ALLCUS-FILE the labeling for ALLCUS.
  1652. 01      ALLCUS-LINE PIC X(80).
  1653. SD      SORT-FILE.
  1654. 01      SORT-LINE.
  1655.         02      LAST-NAME PIC X(20).
  1656.         02      FILLER PIC X(60).
  1657.  
  1658. PROCEDURE DIVISION.
  1659. MAIN-ROUTINE.
  1660.         MERGE SORT-FILE
  1661.                 ASCENDING KEY LAST-NAME
  1662.                 USING OLDCUS-FILE NEWCUS-FILE
  1663.                 GIVING ALLCUS-FILE.
  1664.         STOP RUN.
  1665.   That program creates ALLCUS, which is a combination of OLDCUS 
  1666. and NEWCUS. To do that, the computer must create a SORT-FILE.
  1667.   The word MERGE automatically opens and closes all the files 
  1668. involved. so do not say OPEN or CLOSE.
  1669.   Warning: the word MERGE is in COBOL 74 but not in COBOL 68. So 
  1670. if your computer is old-fashioned and understands just COBOL 68, 
  1671. it doesn't understand the word MERGE.
  1672.   If your computer understands the word MERGE, you can merge as 
  1673. many files as you like. For example, if you have files called 
  1674. CUS1, CUS2, CUS3, and CUS4, you can say:
  1675.         MERGE SORT-FILE
  1676.                 ASCENDING KEY LAST-NAME
  1677.                 USING CUS1-FILE CUS2-FILE CUS3-FILE CUS4-FILE
  1678.                 GIVING ALLCUS-FILE.
  1679. Before you MERGE, make sure that the files you're USING are 
  1680. already in alphabetical order.
  1681.  
  1682.                    Subscripts
  1683.   Like other computer languages, COBOL lets you use subscripts.
  1684.   For example, suppose your 4 favorite friends are SUE, JOE, TOM, 
  1685. and ANN. Let's make FAVORITE-FRIEND (1) be ``SUE'', 
  1686. FAVORITE-FRIEND (2) be ``JOE'', FAVORITE-FRIEND (3) be ``TOM'', 
  1687. and FAVORITE-FRIEND (4) be ``ANN''. Here's how:
  1688. DATA DIVISION.
  1689. WORKING-STORAGE SECTION.
  1690. 01      FAVORITE-FRIEND-TABLE.
  1691.         02      FAVORITE-FRIEND OCCURS 4 TIMES PIC XXX.You have 4 
  1692. FAVORITE-FRIENDs; each has PIC XXX.
  1693.  
  1694. PROCEDURE DIVISION.
  1695. MAIN-ROUTINE.
  1696.         MOVE "SUE" TO FAVORITE-FRIEND (1).
  1697.         MOVE "JOE" TO FAVORITE-FRIEND (2).
  1698.         MOVE "TOM" TO FAVORITE-FRIEND (3).
  1699.         MOVE "ANN" TO FAVORITE-FRIEND (4).
  1700.         DISPLAY FAVORITE-FRIEND (1).
  1701.         DISPLAY FAVORITE-FRIEND (2).
  1702.         DISPLAY FAVORITE-FRIEND (3).
  1703.         DISPLAY FAVORITE-FRIEND (4).
  1704.         STOP RUN.
  1705.   The computer will display:
  1706. SUE
  1707. JOE
  1708. TOM
  1709. ANN
  1710.   When typing the program, remember to put a blank space before 
  1711. the subscript:
  1712.                       FAVORITE-FRIEND (1)
  1713.                                      
  1714.                                      blank space
  1715.   In COBOL, you say ``OCCURS'' instead of ``DIMENSION'':
  1716. BASIC:   DIM F(4)
  1717. FORTRAN: DIMENSION F(4)
  1718. PASCAL:  F: ARRAY [1..4]
  1719. COBOL:   F OCCURS 4 TIMES
  1720.   The subscript can be a variable. For example, instead of saying 
  1721. ___ 
  1722.         DISPLAY FAVORITE-FRIEND (1).
  1723.         DISPLAY FAVORITE-FRIEND (2).
  1724.         DISPLAY FAVORITE-FRIEND (3).
  1725.         DISPLAY FAVORITE-FRIEND (4).
  1726. you can say:
  1727.         DISPLAY FAVORITE-FRIEND (I).
  1728. To do that, you must tell the computer that the I goes from 1 to 
  1729. 4. Here's how:
  1730. DATA DIVISION.
  1731. WORKING-STORAGE SECTION.
  1732. 01      FAVORITE-FRIEND-TABLE.
  1733.         02      FAVORITE-FRIEND OCCURS 4 TIMES PIC XXX.
  1734. 01      I PIC 9.                I is a one-digit number.
  1735.  
  1736. PROCEDURE DIVISION.
  1737. MAIN-ROUTINE.
  1738.         MOVE "SUE" TO FAVORITE-FRIEND (1).
  1739.         MOVE "JOE" TO FAVORITE-FRIEND (2).
  1740.         MOVE "TOM" TO FAVORITE-FRIEND (3).
  1741.         MOVE "ANN" TO FAVORITE-FRIEND (4).
  1742.         PERFORM SHOW-FRIENDSHIP                   
  1743.                 VARYING I FROM 1 BY 1 UNTIL I > 4.I will be 1, 2, 
  1744. 3, 4.
  1745.         STOP RUN.
  1746. SHOW-FRIENDSHIP.                    
  1747.         DISPLAY FAVORITE-FRIEND (I).I is the subscript.
  1748.   To make the program run faster, say ``COMP'' at the end of the 
  1749. subscript's picture:
  1750. ComputerWhat to say
  1751. PDP, Eclipse01      I PIC 9 COMP.
  1752. CDC     01  I PIC 9 COMP-1.
  1753. IBM     01  I PIC 9 COMP SYNC.
  1754. COMP stands for the word COMPUTATIONAL; SYNC stands for the word 
  1755. SYNCHRONIZED.
  1756.  
  1757.   A subscript cannot contain an operation:
  1758. Okay: FAVORITE-FRIEND (3)
  1759. Wrong:FAVORITE-FRIEND (2 + 1)The + is not allowed.
  1760.   Here's how to make Y-TABLE be a table that has 4 rows and 6 
  1761. columns:
  1762. 01      Y-TABLE.
  1763.         02      Y-ROW OCCURS 4 TIMES.
  1764.                 03      Y OCCURS 6 TIMES PIC XXX.
  1765. The entire table is called:
  1766. Y-TABLE
  1767. The first row of Y-TABLE is called:
  1768. Y-ROW (1)
  1769. The second row of Y-TABLE is called:
  1770. Y-ROW (2)
  1771. The entry in the 2nd row and 5th column of Y-TABLE is called:
  1772. Y (2, 5)
  1773.     
  1774.  spaces
  1775.   Test scores Suppose you teach 25 students, you've given each 
  1776. student 4 tests, and you want to put the scores in a table.
  1777.   You want the table to contain 25 rows (a row for each student). 
  1778. In each row, you want the student's first name, middle initial, 
  1779. last name, and 4 scores.
  1780.   Here's how:
  1781. 01      STUDENT-INFORMATION-TABLE.
  1782.         02      STUDENT-INFORMATION-ROW OCCURS 25 TIMES.
  1783.                 03      FIRST-NAME PIC X(15).
  1784.                 03      MIDDLE-INITIAL PIC X.
  1785.                 03      LAST-NAME PIC X(20).
  1786.                 03      TEST-SCORE OCCURS 4 TIMES PIC 999.
  1787.   The entire table is called:
  1788. STUDENT-INFORMATION-TABLE
  1789.   The table contains 25 rows. The first row is called:
  1790. STUDENT-INFORMATION-ROW (1)
  1791. The twelfth row is called:
  1792. STUDENT-INFORMATION-ROW (12)
  1793.   The information in the twelfth row is called:
  1794. FIRST-NAME (12)
  1795. MIDDLE-INITIAL (12)
  1796. LAST-NAME (12)
  1797. TEST-SCORE (12, 1)
  1798. TEST-SCORE (12, 2)
  1799. TEST-SCORE (12, 3)
  1800. TEST-SCORE (12, 4)
  1801.  
  1802.  
  1803.            EXTRA COMMENTS
  1804.                                          Put extra comments in 
  1805. your program, to help your colleagues understand how the program 
  1806. works.
  1807.  
  1808.                                               IDENTIFICATION DIVISION
  1809.                                          The IDENTIFICATION 
  1810. DIVISION can include these paragraphs:
  1811. PROGRAM-ID.
  1812. AUTHOR.
  1813. INSTALLATION.
  1814. DATE-WRITTEN.
  1815. DATE-COMPILED.
  1816. SECURITY.
  1817. In each paragraph after the PROGRAM-ID, put whatever garbage you 
  1818. please. The computer ignores everything the IDENTIFICATION 
  1819. DIVISION says.
  1820.                                          The IDENTIFICATION 
  1821. DIVISION helps the computer center's librarian classify your 
  1822. program. The librarian wants the INSTALLATION paragraph to 
  1823. contain the computer center's name and address, the DATE-WRITTEN 
  1824. paragraph to tell when you finished debugging the program, the 
  1825. DATE-COMPILED paragraph to tell when the computer translated the 
  1826. program from COBOL into machine language, and the SECURITY 
  1827. paragraph to tell who may look at the program and who must not.
  1828.                                          If you put the wrong 
  1829. date in the date-compiled paragraph, don't worry: when you ask 
  1830. the computer to produce a COBOL listing of your program, the 
  1831. listing will automatically show the correct date instead.
  1832.  
  1833.                                                      Asterisks
  1834.                                          The computer ignores any 
  1835. line that begins with an asterisk. So if you put this line in 
  1836. your program ___ 
  1837. *THIS IS A LOUSY PROGRAM
  1838.  ___ the computer will ignore the comment.
  1839.                                          Create comments that 
  1840. explain how your program works. Put the comments near the bottom 
  1841. of the IDENTIFICATION DIVISION, near the top of the PROCEDURE 
  1842. DIVISION, and wherever your program looks confusing.
  1843.                                          On PDP and Eclipse 
  1844. computers, put the asterisk at the far left; don't put any blank 
  1845. spaces before the asterisk. On IBM and CDC computers, put six 
  1846. blank spaces before the asterisk, so that the asterisk is in 
  1847. column 7.